﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Phone.BackgroundTransfer;

namespace MusicOnline
{
    class DownloadHelper
    {
        public bool CreateFolderDownload(string szSavePath = "/shared/transfers")
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.DirectoryExists(szSavePath))
                {
                    isoStore.CreateDirectory(szSavePath);

                    return true;
                }
                else
                    return true;
            }
        }
        public void DownloadInApp(string szUrlFile, string szPath = "/shared/transfers/")
        {
            // Check to see if the maximum number of requests per app has been exceeded.
            if (BackgroundTransferService.Requests.Count() >= 5)
            {
                // Note: Instead of showing a message to the user, you could store the
                // requested file URI in isolated storage and add it to the queue later.
                MessageBox.Show("The maximum number of background file transfer requests for this application has been exceeded. ");
                return;
            }

            // Get the URI of the file to be transferred from the Tag property
            // of the button that was clicked.
            //string transferFileName = ((Button)sender).Tag as string;
            string transferFileName = szUrlFile;
            Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);

            // Create the new transfer request, passing in the URI of the file to 
            // be transferred.
            BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);

            // Set the transfer method. GET and POST are supported.
            transferRequest.Method = "GET";

            // Get the file name from the end of the transfer Uri and create a local Uri 
            // in the "transfers" directory in isolated storage.
            string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
            Uri downloadUri = new Uri(szPath + downloadFile, UriKind.RelativeOrAbsolute);
            transferRequest.DownloadLocation = downloadUri;

            // Pass custom data with the Tag property. This value cannot be more than 4000 characters.
            // In this example, the friendly name for the file is passed. 
            transferRequest.Tag = downloadFile;



            // If the WiFi-only checkbox is not checked, then set the TransferPreferences
            // to allow transfers over a cellular connection.
            //if (wifiOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowCellular;
            //}
            //if (externalPowerOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowBattery;
            //}
            //if (wifiOnlyCheckbox.IsChecked == false && externalPowerOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
            //}

            // Add the transfer request using the BackgroundTransferService. Do this in 
            // a try block in case an exception is thrown.
            try
            {
                BackgroundTransferService.Add(transferRequest);
            }
            catch (InvalidOperationException ex)
            {
                // TBD - update when exceptions are finalized
                MessageBox.Show("Unable to add background transfer request. " + ex.Message);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to add background transfer request.");
            }
        }
        public void DownloadInApp(Uri transferUri, string downloadFile, string szPath = "/shared/transfers/")
        {
            // Check to see if the maximum number of requests per app has been exceeded.
            if (BackgroundTransferService.Requests.Count() >= 5)
            {
                // Note: Instead of showing a message to the user, you could store the
                // requested file URI in isolated storage and add it to the queue later.
                MessageBox.Show("The maximum number of background file transfer requests for this application has been exceeded. ");
                return;
            }

            // Get the URI of the file to be transferred from the Tag property
            // of the button that was clicked.
            //string transferFileName = ((Button)sender).Tag as string;

            // Create the new transfer request, passing in the URI of the file to 
            // be transferred.
            BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);

            // Set the transfer method. GET and POST are supported.
            transferRequest.Method = "GET";

            // Get the file name from the end of the transfer Uri and create a local Uri 
            // in the "transfers" directory in isolated storage.
           // string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
            Uri downloadUri = new Uri(szPath + downloadFile, UriKind.RelativeOrAbsolute);
            transferRequest.DownloadLocation = downloadUri;

            // Pass custom data with the Tag property. This value cannot be more than 4000 characters.
            // In this example, the friendly name for the file is passed. 
            transferRequest.Tag = downloadFile;



            // If the WiFi-only checkbox is not checked, then set the TransferPreferences
            // to allow transfers over a cellular connection.
            //if (wifiOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowCellular;
            //}
            //if (externalPowerOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowBattery;
            //}
            //if (wifiOnlyCheckbox.IsChecked == false && externalPowerOnlyCheckbox.IsChecked == false)
            //{
            //    transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
            //}

            // Add the transfer request using the BackgroundTransferService. Do this in 
            // a try block in case an exception is thrown.
            try
            {
                BackgroundTransferService.Add(transferRequest);
            }
            catch (InvalidOperationException ex)
            {
                // TBD - update when exceptions are finalized
                MessageBox.Show("Unable to add background transfer request. " + ex.Message);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to add background transfer request.");
            }
        }
    }
}
